package org.mushroomdb.transaction;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.mushroomdb.catalog.Column;
import org.mushroomdb.catalog.EncodeableRegister;
import org.mushroomdb.catalog.Register;
import org.mushroomdb.catalog.Table;

/**
 * @author Tomas
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class TrxTable implements TransactionalResource, Table {

    private boolean inTrx = false;
    private Table table;
    //tiene todos los registros
    private List registers = new ArrayList();
    
    //Tiene los registros insertados durante la transaccion
    private List tempRegisters = new ArrayList();
    
    
    public TrxTable(Table table){
        this.table=table;
    }
    
    public void onCommit() {
        this.inTrx=false;
        
        ListIterator iterator = tempRegisters.listIterator();
        while(iterator.hasNext()){
            EncodeableRegister register = (EncodeableRegister) iterator.next();
            this.table.insertRegister(register);
        }

    }

    
    public void onRollback() {
        this.registers = new ArrayList();
        this.tempRegisters = new ArrayList();
    }

    
    public void onBeginTrx() {
        Iterator iter = this.table.getRegisters();
        while(iter.hasNext()){
            Register register = (Register) iter.next();
            this.registers.add(register);
        }
        
        this.inTrx = true;
    }

    
    public boolean isInTrx() {
        return this.inTrx;
    }

    
    public String getName() {
        return this.table.getName();
    }

    
    public void setName(String name) {
       this.table.setName(name);

    }

    
    public Iterator getColumns() {
       return this.table.getColumns();
    }

    
    public void addColumn(Column column) {
        this.table.addColumn(column);
    }

    
    public Column getColumn(String name) {
        return this.table.getColumn(name);
    }

    
    public Iterator getRegisters() {
        return this.registers.listIterator();
    }

    
    //TODO ver como hacer esto
    public Iterator getPages() {
        return this.getPages();
    }

    
    public void insertRegister(EncodeableRegister register) {
        if(this.inTrx){
            this.tempRegisters.add(register);
            this.registers.add(register);
        }
        else{
            this.insertRegister(register);
        }
    }
    
    public long getRegisterCount() {
        if(this.inTrx){
            return new Long(this.registers.size()).longValue();
        }
        else{
            return this.table.getRegisterCount();
        }
    }

    /* (non-Javadoc)
     * @see org.mushroomdb.catalog.Table#deleteRegister(org.mushroomdb.catalog.Register)
     */
    public boolean deleteRegister(Register register) {
        // TODO Auto-generated method stub
        return false;
    }

}
